package cn.iocoder.yudao.module.pplan.service.quarterproducerecord;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pplan.controller.admin.quarterproducerecord.vo.QuarterProduceRecordPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.quarterproducerecord.vo.QuarterProduceRecordSaveReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.quarterproducerecord.vo.QuarterProduceRecordUpdateStatusReqVO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.quarterproducerecord.QuarterProduceRecordDO;
import cn.iocoder.yudao.module.pplan.dal.mysql.quarterproducerecord.QuarterProduceRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pplan.enums.ErrorCodeConstants.QUARTER_PRODUCE_RECORD_NOT_EXISTS;

/**
 * 生产计划-生产季度计划记录 Service 实现类
 *
 * @author yisl
 */
@Service
@Validated
public class QuarterProduceRecordServiceImpl implements QuarterProduceRecordService {

    @Resource
    private QuarterProduceRecordMapper quarterProduceRecordMapper;

    @Override
    public Long createQuarterProduceRecord(QuarterProduceRecordSaveReqVO createReqVO) {
        // 插入
        QuarterProduceRecordDO quarterProduceRecord = BeanUtils.toBean(createReqVO, QuarterProduceRecordDO.class);
        quarterProduceRecordMapper.insert(quarterProduceRecord);
        // 返回
        return quarterProduceRecord.getId();
    }

    @Override
    public void updateQuarterProduceRecord(QuarterProduceRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateQuarterProduceRecordExists(updateReqVO.getId());
        // 更新
        QuarterProduceRecordDO updateObj = BeanUtils.toBean(updateReqVO, QuarterProduceRecordDO.class);
        quarterProduceRecordMapper.updateById(updateObj);
    }

    @Override
    public void updateQuarterProduceStatus(QuarterProduceRecordUpdateStatusReqVO updateReqVO) {
        // 校验存在
        validateQuarterProduceRecordExists(updateReqVO.getId());
        // 更新
        QuarterProduceRecordDO updateObj = BeanUtils.toBean(updateReqVO, QuarterProduceRecordDO.class);
        quarterProduceRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteQuarterProduceRecord(Long id) {
        // 校验存在
        validateQuarterProduceRecordExists(id);
        // 删除
        quarterProduceRecordMapper.deleteById(id);
    }

    @Override
        public void deleteQuarterProduceRecordListByIds(List<Long> ids) {
        // 校验存在
        validateQuarterProduceRecordExists(ids);
        // 删除
        quarterProduceRecordMapper.deleteByIds(ids);
        }

    private void validateQuarterProduceRecordExists(List<Long> ids) {
        List<QuarterProduceRecordDO> list = quarterProduceRecordMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(QUARTER_PRODUCE_RECORD_NOT_EXISTS);
        }
    }

    private void validateQuarterProduceRecordExists(Long id) {
        if (quarterProduceRecordMapper.selectById(id) == null) {
            throw exception(QUARTER_PRODUCE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public QuarterProduceRecordDO getQuarterProduceRecord(Long id) {
        return quarterProduceRecordMapper.selectById(id);
    }

    @Override
    public PageResult<QuarterProduceRecordDO> getQuarterProduceRecordPage(QuarterProduceRecordPageReqVO pageReqVO) {
        return quarterProduceRecordMapper.selectPage(pageReqVO);
    }

}